package zsy.test.设计模式.singleton;

/**
 * 饿汉式：类初始化时就会加载对象，线程天生安全，调用效率高
 *
 * @author zhangshuaiyin
 * @date 2022/8/8 10:14
 */
public class HungarySingleton {

    private static final HungarySingleton INSTANCE = new HungarySingleton();

    private HungarySingleton() {
        if (INSTANCE != null) {
            throw new RuntimeException("防止反射破解单例");
        }
    }

    public static HungarySingleton getInstance() {
        return INSTANCE;
    }

    private Object readResolve() {
        // 防止序列化破解单例
        return INSTANCE;
    }
}
